<?php if (!defined('BASEPATH')) exit ('No direct script access allowed');

class Question extends CI_Controller {
	function __construct(){
		parent::__construct();
		$this->load->library('encrypt');
		$this->load->model('user_model');
		$this->load->model('question_model');
		$this->load->library('form_validation');
		$this->load->library('email');
		
		if(strrpos(current_url() ,"/admin/") > 0 && !isset($this->session->userdata['adminflag'])){
			redirect('/');
		}
	}
	
	public function preview(){
		$id = $this->uri->segment(3);
		$question = $this->question_model->get_question($id);
		$data['title'] = '';
		$data['question'] = $question;
		$this->load->view('question/preview', $data);
	}
	
	public function admin_add(){
		if($this->input->post()){
			$question = $this->getQuestion();
			if($question){
				$this->question_model->question_insert($question);
				$data['message'] = "添加成功！";
				$this->admin_list($data);
				return ;	
			}
		}
		$data['title'] = 'Add Question';
		$this->load->view('templates/header', $data);
		$this->load->view('question/add', $data);
		$this->load->view('templates/footer');
	}
	
	private function getQuestion(){
		$this->form_validation->set_rules('q_section', 'Section', 'trim|required');
		$this->form_validation->set_rules('q_content', 'Question', 'trim|required');
		if($this->form_validation->run() == TRUE){
	
			$str = $this->input->post("q_content");
			$answer = array();
			$answer_str = '';
			preg_match_all('/<strong>([\s\S]*?)<\/strong>/', $str, $mat);
			for($i = 0; $i < count($mat[0]); $i++){
				$str = str_replace($mat[0][$i], '<input type="text" id="answer'.($i + 1).'" name="answer'.($i + 1).'"/>', $str);
				$answer['answer'.($i + 1)] = $mat[1][$i];
				$answer_str .= $mat[1][$i].',';
			}
			$question["title"] = $this->input->post("q_title");
			$question["answer"] = json_encode($answer);
			$question["section"] = $this->input->post("q_section");
			$question["question"] = $str;
			$question['answer_str'] = $answer_str;
			return $question;
		}
		return FALSE;
	}
	
	public function admin_delete(){
		$id = $this->uri->segment(4);
		if($this->input->post()){
			$this->question_model->question_delete($id);
		}
		redirect('/admin/question/list');
	}
	
	public function admin_edit(){
		$data['title'] = 'Edit Question';
		$id = $this->uri->segment(4);
		if($this->input->post()){
			$question = $this->getQuestion();
			if($question){
				$question["id"] = $id;
				$data['message'] = "修改成功！";
				$this->question_model->question_update($question["id"], $question);
			}
		}
		
		$question = $this->question_model->get_question($id);
		$answer = json_decode($question['answer']);
		$str = $question['question'];
		foreach ($answer as $id=>$value) {
		    $str = str_replace('<input type="text" id="'.$id.'" name="'.$id.'"/>', '<strong>'.$value.'</strong>', $str);
		}
		$question['question'] = $str;
		$data['question'] = $question;
		
		$this->load->view('templates/header', $data);
		$this->load->view('question/edit', $data);
		$this->load->view('templates/footer');
	}
	
	public function admin_list($data = array()) {
		$this->load->library('pagination');
		$search = array();
		if($this->input->post()){
			$search['q_section'] = $this->input->post('q_section');
			$search['q_answer_str'] = $this->input->post('q_answer_str');
			$search['sort'] = $this->input->post('sort');
			$search['desc'] = $this->input->post('desc');
		}
		
		$total = $this->question_model->count_question($search);
		$config = $this->get_page_config('index.php/admin/question/list/', $total);
		$offset = $this->uri->segment(4);
		$offset = $offset?$offset:0;
		
		$data['results'] = $this->question_model->list_question($search, $config['per_page'], $offset);
		$data['offset'] = $offset;
		$data['search'] = $search;
		
		$this->pagination->initialize($config); 
		$this->load->view('templates/header', $data);
		$this->load->view('question/list', $data);
		$this->load->view('templates/footer');
	}
	
	private function get_page_config($uri, $total){
		$config['base_url'] = base_url().$uri;
		$config['total_rows'] = $total;
		$config['per_page'] = 5; 
		$config['num_links'] = 2; 
		$config['uri_segment'] = 4;
		
		$config['full_tag_open'] = '<ul class="pagination">';
		$config['full_tag_close'] = '</ul>';
		
		$config['first_link'] = '&lt;&lt;';
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		
		$config['next_link'] = '&gt;';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		
		$config['prev_link'] = '&lt;';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		
		$config['last_link'] = '&gt;&gt;';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		
		$config['cur_tag_open'] = '<li class="selected"><a href="#">';
		$config['cur_tag_close'] = '</a></li>';
		return $config;
	}
}
